/*

  Simple cross-product, outputs in correct triangular form.

  - Coalescing memory access in the Column reads
  - Memory coalescing in the Row reads
  - 50% coalescing in the write (float8s)
  - Shared memory reads of type float to reduce global memory traffic
  - Each thread works on a 2x2 square of data

 */

#define LOAD_TWO_BY_TWO_FLOAT(s)		\
  {*(input##s##_p) = *array_p;			\
    CUBE_ADD_BYTES(4);}
   

#define TWO_BY_TWO_COMPUTE_FLOAT(s)				\
  {float col1Xreal = input[s][colIdx + 0];		\
  float col1Ximag = input[s][colIdx + 1];		\
  float col1Yreal = input[s][colIdx + 2];		\
  float col1Yimag = input[s][colIdx + 3];		\
  float col2Xreal = input[s][colIdx + 4];		\
  float col2Ximag = input[s][colIdx + 5];		\
  float col2Yreal = input[s][colIdx + 6];		\
  float col2Yimag = input[s][colIdx + 7];		\
  float row1Xreal = input[s][rowIdx + 0];		\
  float row1Ximag = input[s][rowIdx + 1];		\
  float row1Yreal = input[s][rowIdx + 2];		\
  float row1Yimag = input[s][rowIdx + 3];		\
  float row2Xreal = input[s][rowIdx + 4];		\
  float row2Ximag = input[s][rowIdx + 5];		\
  float row2Yreal = input[s][rowIdx + 6];		\
  float row2Yimag = input[s][rowIdx + 7];		\
  sum11XXreal += row1Xreal * col1Xreal;			\
  sum11XXreal += row1Ximag * col1Ximag;			\
  sum11XXimag += row1Ximag * col1Xreal;			\
  sum11XXimag -= row1Xreal * col1Ximag;			\
  sum11XYreal += row1Xreal * col1Yreal;			\
  sum11XYreal += row1Ximag * col1Yimag;			\
  sum11XYimag += row1Ximag * col1Yreal;			\
  sum11XYimag -= row1Xreal * col1Yimag;			\
  sum11YXreal += row1Yreal * col1Xreal;			\
  sum11YXreal += row1Yimag * col1Ximag;			\
  sum11YXimag += row1Yimag * col1Xreal;			\
  sum11YXimag -= row1Yreal * col1Ximag;			\
  sum11YYreal += row1Yreal * col1Yreal;			\
  sum11YYreal += row1Yimag * col1Yimag;			\
  sum11YYimag += row1Yimag * col1Yreal;			\
  sum11YYimag -= row1Yreal * col1Yimag;			\
  sum12XXreal += row1Xreal * col2Xreal;			\
  sum12XXreal += row1Ximag * col2Ximag;			\
  sum12XXimag += row1Ximag * col2Xreal;			\
  sum12XXimag -= row1Xreal * col2Ximag;			\
  sum12XYreal += row1Xreal * col2Yreal;			\
  sum12XYreal += row1Ximag * col2Yimag;			\
  sum12XYimag += row1Ximag * col2Yreal;			\
  sum12XYimag -= row1Xreal * col2Yimag;			\
  sum12YXreal += row1Yreal * col2Xreal;			\
  sum12YXreal += row1Yimag * col2Ximag;			\
  sum12YXimag += row1Yimag * col2Xreal;			\
  sum12YXimag -= row1Yreal * col2Ximag;			\
  sum12YYreal += row1Yreal * col2Yreal;			\
  sum12YYreal += row1Yimag * col2Yimag;			\
  sum12YYimag += row1Yimag * col2Yreal;			\
  sum12YYimag -= row1Yreal * col2Yimag;			\
  sum21XXreal += row2Xreal * col1Xreal;			\
  sum21XXreal += row2Ximag * col1Ximag;			\
  sum21XXimag += row2Ximag * col1Xreal;			\
  sum21XXimag -= row2Xreal * col1Ximag;			\
  sum21XYreal += row2Xreal * col1Yreal;			\
  sum21XYreal += row2Ximag * col1Yimag;			\
  sum21XYimag += row2Ximag * col1Yreal;			\
  sum21XYimag -= row2Xreal * col1Yimag;			\
  sum21YXreal += row2Yreal * col1Xreal;			\
  sum21YXreal += row2Yimag * col1Ximag;			\
  sum21YXimag += row2Yimag * col1Xreal;			\
  sum21YXimag -= row2Yreal * col1Ximag;			\
  sum21YYreal += row2Yreal * col1Yreal;			\
  sum21YYreal += row2Yimag * col1Yimag;			\
  sum21YYimag += row2Yimag * col1Yreal;			\
  sum21YYimag -= row2Yreal * col1Yimag;			\
  sum22XXreal += row2Xreal * col2Xreal;			\
  sum22XXreal += row2Ximag * col2Ximag;			\
  sum22XXimag += row2Ximag * col2Xreal;			\
  sum22XXimag -= row2Xreal * col2Ximag;			\
  sum22XYreal += row2Xreal * col2Yreal;			\
  sum22XYreal += row2Ximag * col2Yimag;			\
  sum22XYimag += row2Ximag * col2Yreal;			\
  sum22XYimag -= row2Xreal * col2Yimag;			\
  sum22YXreal += row2Yreal * col2Xreal;			\
  sum22YXreal += row2Yimag * col2Ximag;			\
  sum22YXimag += row2Yimag * col2Xreal;			\
  sum22YXimag -= row2Yreal * col2Ximag;			\
  sum22YYreal += row2Yreal * col2Yreal;			\
  sum22YYreal += row2Yimag * col2Yimag;			\
  sum22YYimag += row2Yimag * col2Yreal;			\
  sum22YYimag -= row2Yreal * col2Yimag;}

CUBE_KERNEL(shared2x2float, cuFloatComplex *array_d, cuFloatComplex *product_d,
	    const int write)

{
  CUBE_START;

  //get local thread ID
  int ty = threadIdx.y;
  int tx = threadIdx.x;
  int tid = ty*TILE_WIDTH + tx;

  //set frequency number from blockIdx.y
  //int f = blockIdx.y;

  int Row, Col, blockX, blockY;
  CUBE_DEVICE_CALL(findPosition, Col, Row, blockX, blockY);
  Col*=2; Row*=2;

  //declare shared memory for input coalescing
  #define PAD 1
  __shared__ float input[4][8*(2*TILE_WIDTH+PAD)]; // 8 = C x P x 2, 2 from row + col 

  //instantiate sum variables
  float sum11XXreal = 0.0, sum11XXimag = 0.0;
  float sum11XYreal = 0.0, sum11XYimag = 0.0;
  float sum11YXreal = 0.0, sum11YXimag = 0.0;
  float sum11YYreal = 0.0, sum11YYimag = 0.0;
  float sum12XXreal = 0.0, sum12XXimag = 0.0;
  float sum12XYreal = 0.0, sum12XYimag = 0.0;
  float sum12YXreal = 0.0, sum12YXimag = 0.0;
  float sum12YYreal = 0.0, sum12YYimag = 0.0;
  float sum21XXreal = 0.0, sum21XXimag = 0.0;
  float sum21XYreal = 0.0, sum21XYimag = 0.0;
  float sum21YXreal = 0.0, sum21YXimag = 0.0;
  float sum21YYreal = 0.0, sum21YYimag = 0.0;
  float sum22XXreal = 0.0, sum22XXimag = 0.0;
  float sum22XYreal = 0.0, sum22XYimag = 0.0;
  float sum22YXreal = 0.0, sum22YXimag = 0.0;
  float sum22YYreal = 0.0, sum22YYimag = 0.0;

  float* array_p = (float*)array_d + blockIdx.y*NSTATION*4 + tid;
  if  (tid < 8*TILE_WIDTH) {
    array_p += 2*blockX*TILE_WIDTH*4;
  } else {
    array_p += 2*blockY*TILE_HEIGHT*4 -8*TILE_WIDTH;
  }

  int colIdx = (tx/4)*(2*TILE_WIDTH+PAD) + 8*(tx%4);
  int rowIdx = (ty/4)*(2*TILE_WIDTH+PAD) + 8*(ty%4) + 4*(2*TILE_WIDTH+PAD);

  // no longer the true tid
  tid = (2*TILE_WIDTH + PAD)*(ty/2) + TILE_WIDTH*(ty%2) + tx;

  float *input0_p = input[0] + tid;
  float *input1_p = input[1] + tid;
  float *input2_p = input[2] + tid;
  float *input3_p = input[3] + tid;

  LOAD_TWO_BY_TWO_FLOAT(0);
  array_p += NFREQUENCY*NSTATION*4;
  LOAD_TWO_BY_TWO_FLOAT(1);
  array_p += NFREQUENCY*NSTATION*4;

  //#pragma unroll 4
  for(int t=0; t<NTIME-4; t+=4){
    __syncthreads();
    TWO_BY_TWO_COMPUTE_FLOAT(0);
    TWO_BY_TWO_COMPUTE_FLOAT(1);

    LOAD_TWO_BY_TWO_FLOAT(2);
    array_p += NFREQUENCY*NSTATION*4;
    LOAD_TWO_BY_TWO_FLOAT(3);
    array_p += NFREQUENCY*NSTATION*4;

    __syncthreads();

    TWO_BY_TWO_COMPUTE_FLOAT(2);
    TWO_BY_TWO_COMPUTE_FLOAT(3);

    LOAD_TWO_BY_TWO_FLOAT(0);
    array_p += NFREQUENCY*NSTATION*4;
    LOAD_TWO_BY_TWO_FLOAT(1);
    array_p += NFREQUENCY*NSTATION*4;

  } 

  __syncthreads();

  TWO_BY_TWO_COMPUTE_FLOAT(0);
  TWO_BY_TWO_COMPUTE_FLOAT(1);

  LOAD_TWO_BY_TWO_FLOAT(2);
  array_p += NFREQUENCY*NSTATION*4;
  LOAD_TWO_BY_TWO_FLOAT(3);

  __syncthreads();

  TWO_BY_TWO_COMPUTE_FLOAT(2);
  TWO_BY_TWO_COMPUTE_FLOAT(3);

  if (write)
    CUBE_DEVICE_CALL(write2x2, Col, Row, product_d, sum11XXreal, sum11XXimag, sum11XYreal, 
		     sum11XYimag, sum11YXreal, sum11YXimag, sum11YYreal, sum11YYimag, 
		     sum12XXreal, sum12XXimag, sum12XYreal, sum12XYimag, sum12YXreal, 
		     sum12YXimag, sum12YYreal, sum12YYimag, sum21XXreal, sum21XXimag, 
		     sum21XYreal, sum21XYimag, sum21YXreal, sum21YXimag, sum21YYreal, 
		     sum21YYimag, sum22XXreal, sum22XXimag, sum22XYreal, sum22XYimag, 
		     sum22YXreal, sum22YXimag, sum22YYreal, sum22YYimag);

  if (Col < Row) {
    CUBE_ADD_BYTES(128);
    CUBE_ADD_FLOPS(NTIME*128);
  } else if (Col == Row) {
    CUBE_ADD_BYTES(96);
    CUBE_ADD_FLOPS(NTIME*96);
  }

  CUBE_END;
}
